﻿<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars2d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

    <meta name="keywords" content="火星科技,leaflet,mars2d,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 leaflet leaflet开发 leaflet框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.jsdelivr.net/gh/muyao1987/cdn/marsgis.cn/favicon/favicon.ico" />
    <title>graphic 数据图层 | Mars2D | 地理信息平台 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,bootstrap-checkbox,layer,haoutil,mars2d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
    <link href="css/divGraphic.css" rel="stylesheet" />
  </head>
  <body>
    <div id="mars2dContainer" class="mars2d-container"></div>

    <div class="infoview">
      <div>
        图层状态：
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkShow" class="styled" type="checkbox" checked />
          <label for="chkShow"> 显示隐藏 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkPopup" class="styled" type="checkbox" checked />
          <label for="chkPopup"> Popup绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkTooltip" class="styled" type="checkbox" />
          <label for="chkTooltip"> Tooltip绑定 </label>
        </div>
        <div class="checkbox checkbox-primary checkbox-inline">
          <input id="chkContextMenu" class="styled" type="checkbox" checked />
          <label for="chkContextMenu"> 右键菜单绑定 </label>
        </div>
      </div>

      <div>
        <span>点状(单个坐标)：</span>
        <a href="editor.html#f10_marker" target="_blank" class="btn btn-primary">图标点</a>
        <a href="editor.html#f10_movingMarker" target="_blank" class="btn btn-primary">运动点</a>
        <a href="editor.html#f10_point" target="_blank" class="btn btn-primary">像素点</a>
        <a href="editor.html#f10_label" target="_blank" class="btn btn-primary">文本</a>
        <a href="editor.html#f10_divGraphic" target="_blank" class="btn btn-primary">DIV点</a>
        <a href="editor.html#f10_circle" target="_blank" class="btn btn-primary">圆</a>
        <a href="editor.html#f10_ellipse" target="_blank" class="btn btn-primary">椭圆</a>
      </div>
      <div>
        <span>线面状(多个坐标)：</span>
        <a href="editor.html#f15_polyline" target="_blank" class="btn btn-primary">线</a>
        <a href="editor.html#f15_polyline_offset" target="_blank" class="btn btn-primary">平行线</a>
        <a href="editor.html#f15_rectangle" target="_blank" class="btn btn-primary">矩形</a>
        <a href="editor.html#f15_image" target="_blank" class="btn btn-primary">图片</a>
        <a href="editor.html#f15_polygon" target="_blank" class="btn btn-primary">面</a>
      </div>
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {
          zoom: 13,
          center: { lng: 117.251587, lat: 31.81748 },
        });

        //创建地图
        map = new mars2d.Map("mars2dContainer", mapOptions);

        //1.在map上绑定监听事件
        // map.on(mars2d.EventType.click, function(event) {
        //   console.log('监听map，单击了矢量对象', event)
        // })

        //创建矢量数据图层
        let graphicLayer = new mars2d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        //图层管理的相关处理，
        //2.在layer上绑定监听事件
        initLayerManager(graphicLayer);

        //加一些演示数据
        addGraphic_01(graphicLayer);
        addGraphic_02(graphicLayer);
        addGraphic_03(graphicLayer);
        addGraphic_04(graphicLayer);
        addGraphic_05(graphicLayer);
        addGraphic_06(graphicLayer);
        addGraphic_07(graphicLayer);
        addGraphic_08(graphicLayer);
        addGraphic_09(graphicLayer);
        addGraphic_10(graphicLayer);
        addGraphic_11(graphicLayer);
        addGraphic_12(graphicLayer);
      }

      // 图标点
      function addGraphic_01(graphicLayer) {
        var graphic = new mars2d.graphic.Marker({
          latlng: [31.854628, 117.245425],
          style: {
            image: "img/marker/mark1.png",
            width: 32,
            height: 44,
            // rotationAngle: 90,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      //运动点
      function addGraphic_02(graphicLayer) {
        let latlngs = [
          [31.85044, 117.102757],
          [31.851607, 117.143269],
          [31.852919, 117.185154],
          [31.857002, 117.216396],
          [31.851607, 117.233219],
          [31.854085, 117.253132],
          [31.872893, 117.252102],
          [31.872164, 117.236652],
          [31.873184, 117.22086],
          [31.875663, 117.21365],
        ];
        var movingMarker = new mars2d.graphic.MovingMarker({
          latlngs: latlngs,
          durations: [3000, 9000, 9000, 4000, 4000, 4000, 4000, 4000, 4000, 4000],
          style: {
            image: "img/marker/bike.png",
            iconSize: [25, 39],
            autostart: true,
          },
        });
        graphicLayer.addGraphic(movingMarker);

        //放个线参考
        var graphic = new mars2d.graphic.Polyline({
          latlngs: latlngs,
          style: {
            color: "#0000ff",
            width: 2,
          },
        });
        graphicLayer.addGraphic(graphic);

        movingMarker.on("end", function () {
          movingMarker.bindPopup("<b>运动完成了!</b>", { closeOnClick: false }).openPopup();
        });
      }

      // 像素点
      function addGraphic_03(graphicLayer) {
        var graphic = new mars2d.graphic.Point({
          latlng: L.latLng(31.842639, 117.225795),
          style: {
            pixelSize: 10,
            // fill: true,
            color: "#00ffff",
            opacity: 0.3,
            outline: true,
            outlineColor: "#00ff00",
            outlineOpacity: 1.0,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 文本
      function addGraphic_04(graphicLayer) {
        var graphic = new mars2d.graphic.Label({
          latlng: L.latLng(31.818606, 117.296904),
          style: {
            text: "安徽合肥欢迎您",
            color: "#0000FF",
            font_size: 25,
            font_family: "楷体",
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      //一个黑色面板，指向左下角黄色连线
      function addGraphic_05(graphicLayer) {
        var graphic = new mars2d.graphic.DivGraphic({
          latlng: [31.822294, 117.221761],
          style: {
            html: `<div class="marsBlackPanel  animation-spaceInDown">
                    <div class="marsBlackPanel-text">大湖名城,创新高地</div>
                </div>`,
          },
        });
        graphicLayer.addGraphic(graphic);

        // graphic.testPoint = true; //打开测试点，与DIV点进行对比位置调整css
      }

      // 圆
      function addGraphic_06(graphicLayer) {
        var graphic = new mars2d.graphic.Circle({
          latlng: L.latLng(31.818606, 117.296904),
          style: {
            radius: 2500, //单位：米
            fill: true,
            fillColor: "#ff0000",
            fillOpacity: 0.3,
            outline: false,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 椭圆
      function addGraphic_07(graphicLayer) {
        var graphic = new mars2d.graphic.Ellipse({
          latlng: [31.81748, 117.251587],
          style: {
            semiMinorAxis: 1000, //单位：米
            semiMajorAxis: 2000,
            rotation: 0,

            fill: true,
            fillColor: "#00ffff",
            fillOpacity: 0.3,
            outline: true,
            outlineWidth: 2,
            outlineColor: "#00ff00",
            outlineOpacity: 0.5,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 线
      function addGraphic_08(graphicLayer) {
        var colors = ["rgb(255, 0, 100)", "rgb(20, 200, 100)", "rgb(0, 0, 255)"];

        var graphic = new mars2d.graphic.Polyline({
          latlngs: [
            [31.809299, 117.163284],
            [31.777104, 117.169052],
            [31.764332, 117.211006],
          ],
          style: {
            width: 4,
            gradientColors: colors,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 平行线
      function addGraphic_09(graphicLayer) {
        var coords = [
          [31.853069, 117.154857],
          [31.848546, 117.15404],
          [31.846285, 117.153482],
          [31.844166, 117.151462],
          [31.84183, 117.150301],
          [31.839572, 117.151505],
          [31.836659, 117.153999],
          [31.834728, 117.155159],
          [31.833051, 117.155461],
        ];

        var middleLine = new mars2d.graphic.Polyline({
          latlngs: coords,
          style: {
            width: 1,
            dashArray: "5,10",
            color: "black",
            opacity: 0.3,
          },
        });
        graphicLayer.addGraphic(middleLine);

        var leftLine = new mars2d.graphic.Polyline({
          latlngs: coords,
          style: {
            color: "#f00",
            opacity: 1,
            offset: -6,
          },
        });
        graphicLayer.addGraphic(leftLine);

        var rightLine = new mars2d.graphic.Polyline({
          latlngs: coords,
          style: {
            color: "#080",
            opacity: 1,
            offset: 6,
          },
        });
        graphicLayer.addGraphic(rightLine);
      }

      // 矩形
      function addGraphic_10(graphicLayer) {
        let latlngs = [
          [31.870639, 117.271662],
          [31.8491, 117.247705],
        ];
        var graphic = new mars2d.graphic.Rectangle({
          latlngs: latlngs,
          style: {
            fill: true,
            fillColor: "#3388ff",
            fillOpacity: 0.3,
            outline: true,
            outlineWidth: 2,
            outlineColor: "#0000FF",
            outlineOpacity: 0.5,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 图片
      function addGraphic_11(graphicLayer) {
        var graphic = new mars2d.graphic.Image({
          latlngs: mars2d.PointTrans.coords2latlngs([
            [117.170734, 31.788157],
            [117.170734, 31.823752],
            [117.206955, 31.823752],
            [117.206955, 31.788157],
            [117.170734, 31.788157],
          ]),
          style: {
            opacity: 1,
            stroke: false,
            fill: true,
            fillColor: "#ffffff",
            fillOpacity: 0.01,
            url: "img/simple/gugong.jpg",
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      // 水面
      function addGraphic_12(graphicLayer) {
        //经纬度 转为leafelt的纬度、经度标准
        let latlngs = mars2d.PointTrans.coords2latlngs([
          [117.216386, 31.815376, 35.16],
          [117.222533, 31.81729, 29.21],
          [117.22642, 31.814983, 28.43],
          [117.22681, 31.810739, 28.55],
          [117.212868, 31.811302, 34.4],
          [117.212538, 31.81424, 31.87],
          [117.214681, 31.81402, 32.97],
        ]);
        var graphic = new mars2d.graphic.Polygon({
          latlngs: latlngs,
          style: {
            fill: true,
            image: "img/fill/movingRiver.png",
            outline: true,
            outlineWidth: 6,
            outlineColor: "#00ffff",
            outlineOpacity: 1.0,
          },
        });
        graphicLayer.addGraphic(graphic);
      }

      //在图层级处理一些事物
      function initLayerManager(graphicLayer) {
        //在layer上绑定监听事件
        graphicLayer.on(mars2d.EventType.click, function (event) {
          console.log("监听layer，单击了矢量对象", event);
        });

        //可在图层上绑定popup,对所有加到这个图层的矢量数据都生效
        bindLayerPopup(graphicLayer);

        $("#chkPopup").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            bindLayerPopup(graphicLayer);
          } else {
            graphicLayer.unbindPopup();
          }
        });

        //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效
        $("#chkTooltip").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            graphicLayer.bindTooltip("我是layer上绑定的Tooltip");
          } else {
            graphicLayer.unbindTooltip();
          }
        });

        //可在图层绑定右键菜单,对所有加到这个图层的矢量数据都生效
        bindLayerContextMenu(graphicLayer);
        $("#chkContextMenu").change(function () {
          let val = $(this).is(":checked");

          if (val) {
            bindLayerContextMenu(graphicLayer);
          } else {
            graphicLayer.unbindContextMenu(true);
          }
        });

        $("#chkHasEdit").change(function () {
          let val = $(this).is(":checked");

          graphicLayer.hasEdit = val; //启用编辑
        });

        $("#chkShow").change(function () {
          let val = $(this).is(":checked");

          graphicLayer.show = val; //显示隐藏
        });

        $("#btnClear").click(function () {
          graphicLayer.clear();
        });

        $("#btnExpFile").click(function () {
          if (graphicLayer.length === 0) {
            window.layer.msg("当前没有标注任何数据，无需保存！");
            return;
          }

          let geojson = graphicLayer.toGeoJSON();
          haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson));
        });

        $("#btnImpFile").click(function () {
          $("#input_draw_file").click();
        });

        function clearSelectFile() {
          if (!window.addEventListener) {
            document.getElementById("input_draw_file").outerHTML += ""; //IE
          } else {
            document.getElementById("input_draw_file").value = ""; //FF
          }
        }

        $("#input_draw_file").change(function (e) {
          let file = this.files[0];

          let fileName = file.name;
          let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase();

          if (fileType == "json" || fileType == "geojson") {
            let reader = new FileReader();
            reader.readAsText(file, "UTF-8");
            reader.onloadend = function (e) {
              let json = this.result;
              graphicLayer.loadGeoJSON(json, {
                flyTo: true,
              });
              clearSelectFile();
            };
          } else {
            window.layer.msg("暂不支持 " + fileType + " 文件类型的数据！");
            clearSelectFile();
          }
        });
      }

      function bindLayerPopup(graphicLayer) {
        graphicLayer.bindPopup(function (event) {
          let attr = event.graphic?.attr || {};
          attr.test1 = "测试属性";
          // attr["视频"] = `<video src='http://data.mars2d.cn/file/video/lukou.mp4' controls autoplay style="width: 300px;" ></video>`;

          return mars2d.Util.getTemplateHtml({ title: "layer上绑定的Popup", template: "all", attr: attr });
        });
      }

      function bindLayerContextMenu(graphicLayer) {
        graphicLayer.bindContextMenu([
          {
            text: "开始编辑对象",
            iconCls: "fa fa-edit",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic || !graphic.editing) {
                return false;
              }
              return !graphic.editing.enabled();
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic && graphic.editing) {
                graphic.editing.enable();
              }
            },
          },
          {
            text: "停止编辑对象",
            iconCls: "fa fa-edit",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic || !graphic.editing) {
                return false;
              }
              return graphic.editing.enabled();
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic && graphic.editing) {
                graphic.editing.disable();
              }
            },
          },
          {
            text: "删除对象",
            iconCls: "fa fa-trash-o",
            show: (event) => {
              let graphic = event.graphic;
              if (!graphic) {
                return false;
              } else {
                return true;
              }
            },
            callback: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return;
              }
              graphicLayer.removeGraphic(graphic);
            },
          },
          {
            text: "计算长度",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "polyline" || graphic.type === "brushLine";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars2d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的长度为:" + strDis);
            },
          },
          {
            text: "计算周长",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars2d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的周长为:" + strDis);
            },
          },
          {
            text: "计算面积",
            iconCls: "fa fa-reorder",
            show: function (e) {
              let graphic = e.graphic;
              if (!graphic) {
                return false;
              }
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strArea = mars2d.MeasureUtil.formatArea(graphic.area);
              haoutil.alert("该对象的面积为:" + strArea);
            },
          },
        ]);
      }
    </script>
  </body>
</html>
